import numpy as np
import cv2, threading, time, sys
from socket import *

# 统计接收速度，_qr_set_size为目前接收数据量的大小
_qr_set_size = 0

# _qr_set用于存放接受照片的数据，set数据结构用于存放不重复数据，对存入的数据会自动去重
_qr_set = set()
#统计接受照片个数
count_images = 0

s = socket(AF_INET, SOCK_DGRAM,0)
addr = ('0.0.0.0', 9090)
s.bind(addr)


_start_time = time.time()
while True:
    data, _ = s.recvfrom(921600)
    receive_data = np.frombuffer(data, dtype='uint8')
    r_img = cv2.imdecode(receive_data, 1)

    #计算
    _data_len = len(r_img)

    # 判断是否接受到照片
    if _data_len > 0:
        #统计照片个数
        count_images += 1
        # 统计累计接收数据大小
        _qr_set_size = _qr_set_size + sys.getsizeof(receive_data) / 1024
        # 获取当前时间time.time()，减去开始时间_start_time，使用round保留两位小数
    _time_span = round(time.time() - _start_time, 2)

    # 打印接受速率
    print("count_images:{}" .format(count_images))
    print("%.3fkb/s" % (_qr_set_size / _time_span))

    cv2.putText(r_img, "server", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)

    cv2.imshow('server_frame', r_img)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cv2.destroyAllWindows()


